package servers;

import interfaces.CommServerInterface;
import java.rmi.RemoteException;
import java.util.HashMap;







/**
 * Classe du serveur de communication
 *
 */
public class CommServer implements CommServerInterface {


	/** HashMap (Place de comm --> NbToken disponibles) */
	private HashMap<String, Integer> places = new HashMap<String, Integer>();

	
	/**
	 * Initialiser chaque place de communication 
	 * au nombre de jetons qu'elle contient 
	 */
	public CommServer() {		
		
		/** Place de communication Mutex */
		places.put("Mutex", new Integer(1));		
	}
	
	
	/* (non-Javadoc)
	 * @see interfaces.commServerInterface#consumeTokens(java.lang.String, int)
	 */
	synchronized  public boolean consumeTokens(String namePlace, int nbToken) throws RemoteException{
		
		if(this.places.get(namePlace) >= nbToken){
			
			this.places.put(namePlace, this.places.get(namePlace) - nbToken);

			return true;
		}
		
		return false;
	}

	/* (non-Javadoc)
	 * @see interfaces.commServerInterface#produceTokens(java.lang.String, int)
	 */
	synchronized public boolean produceTokens(String name_place, int nb_token) throws RemoteException{
			this.places.put(name_place, this.places.get(name_place) + nb_token);
		return true;
	}
	
}
